<?php
namespace Vioscope;

if ( ! class_exists( '\Vioscope\Video' ) ) :

class Video {
	// YT video id
	public $id;
	// Raw json parsed data
	public $data;
	// Processed data
	public $info;
	// Video formats available
	public $formats;

	function __construct( $video_id = null ) {
		if ( $video_id ) {
			$this->set_video_id( $video_id );
		}
	}

	public function set_video_id( $video_id = null ) {
		$this->id = $video_id;
		// Clear previous data (if any)
		unset($this->data);
		unset($this->info);
		unset($this->formats);
		// Update data
		$this->fetch_data($this-id);
	}

	public function fetch_data( $video_id = null ) {
		if ( ! $video_id ) {
			$video_id = $this->id;
		}
		parse_str( file_get_contents( "https://youtube.com/get_video_info?html5=1&gl=US&hl=en&video_id={$this->id}" ), $info );
		$this->data = json_decode( $info['player_response'], true );
		
		// Other info
		$this->info['id'] = $this->id;
		$this->info['title'] = $this->data['videoDetails']['title'];
		$this->info['length'] = $this->data['videoDetails']['lengthSeconds'];
		$this->info['keywords'] = $this->data['videoDetails']['keywords'];
		$this->info['channelId'] = $this->data['videoDetails']['channelId'];
		$this->info['channel_url'] = get_local_url( $this->data['videoDetails']['channelId'], 'channel' );
		$this->info['short_description'] = urlify_string( replace_urls_with_local_urls( $this->data['videoDetails']['shortDescription'] ) );
		$this->info['thumbnails'] = $this->data['videoDetails']['thumbnail']['thumbnails'];
		$this->info['averageRating'] = $this->data['videoDetails']['averageRating'];
		$this->info['views'] = $this->data['videoDetails']['viewCount'];
		$this->info['author'] = $this->data['videoDetails']['author'];
		$this->info['isPrivate'] = ( $this->data['videoDetails']['isPrivate'] == '' ) ? false : true;
		$this->info['isLive'] = $this->data['videoDetails']['isLiveContent'];
		$this->info['isUnlisted'] = ( $this->data['videoDetails']['isUnlisted'] == '' ) ? false : true;
		$this->info['category'] = $this->data['microformat']['playerMicroformatRenderer']['category'];
		$this->info['publish_date'] = $this->data['microformat']['playerMicroformatRenderer']['publishDate'];
		$this->info['publish_date_friendly'] = time_ago( $this->data['microformat']['playerMicroformatRenderer']['publishDate'] );
		// useful...??
		$this->info['ownerChannelName'] = $this->data['microformat']['playerMicroformatRenderer']['ownerChannelName'];
		$this->info['uploadDate'] = $this->data['microformat']['playerMicroformatRenderer']['uploadDate'];
		// Video formats available
		$this->info['formats'] = $this->get_formats();
	}

	private function get_formats() {
		if ( ! isset( $this->data ) ) return;
		$formats = array();
		foreach( array_merge( $this->data['streamingData']['formats'], $this->data['streamingData']['adaptiveFormats'] ) as $format ) {
			$formats[] = array(
				'url' => $format['url'],
				'mimetype' => $format['mimeType'],
				'mimetype_type' => explode( ";", $format['mimeType'] )[0],
				'bitrate' => $format['bitrate'],
				'width' => $format['width'],
				'height' => $format['height'],
				'is_audio_only' => ( strpos( $format['mimeType'], 'audio/' ) ==! false && ! empty( $format['audioChannels'] ) ) ? true : false,
				'is_video_only' => ( strpos( $format['mimeType'], 'video/' ) ==! false && empty( $format['audioChannels'] ) ) ? true : false,
				'bitrate' => $format['bitrate'],
				'quality' => $format['quality'],
				'fps' => $format['fps'],
				'quality_label' => $format['qualityLabel'],
				'approx_duration_ms' => $format['approxDurationMs'],
				'audio_sample_rate' => $format['audioSampleRate'],
				'audio_channels' => $format['audioChannels'],
			);
		}
		return $formats;
	}
}

endif;
